package com.gnerv.pabu.plugin.config;

import com.gnerv.pabu.plugin.entity.TemplateGroup;
import com.gnerv.pabu.plugin.utils.TemplateGroupUtils;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;

@Slf4j
@Data
@State(name = "PabuMybatisPlusSettings", storages = @Storage("pabu-mybatis-plus-settings.xml"))
public class MybatisPlusSettings implements PersistentStateComponent<MybatisPlusSettings> {

    public static final String GROUP_NAME = "MybatisPlus";
    public static final String GROUP_PATH = "/template/MybatisPlus/";
    public static final String[] TEMPLATES = new String[]{"controller.java", "service.java", "serviceImpl.java", "mapper.java", "mapper.xml" };
    public static final String TYPE = ".vm";

    private TemplateGroup templateGroup;

    public MybatisPlusSettings() {
        try {
            templateGroup = TemplateGroupUtils.loadTemplate(GROUP_NAME, GROUP_PATH, TEMPLATES, TYPE);
        } catch (IOException e) {
            log.info("加载 MybatisPlus 模板失败", e);
        }
    }

    @Nullable
    @Override
    public MybatisPlusSettings getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull MybatisPlusSettings mybatisPlusSettings) {
        XmlSerializerUtil.copyBean(mybatisPlusSettings, this);
    }
}